package cn.jiyun.controller;

import cn.jiyun.client.UserClient;
import cn.jiyun.pojo.Order;
import cn.jiyun.pojo.User;
import cn.jiyun.service.OrderService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("order")
@RestController

public class OrderController {
    @Resource
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient client;

    @GetMapping("{id}")
    public Order findByIds(@PathVariable Long id){
        //System.out.println(Truth);
       // System.out.println(Truth);
        Order order=orderService.findById(id);
        //User user=client.findByid(order.getUserId());
        //order.setUser(user);
        return order;
    }

    @GetMapping("/hx")
    public Order findById(@RequestParam("id") Long id){

        Order order = orderService.findById(id);
        User user = client.findByid(order.getUserId());
        order.setUser(user);
        return order;
    }

    @PostMapping("edit")
    public void edit(@RequestBody Order order){
        orderService.edit(order);
    }

    @GetMapping("/all")
    public Page<Order> all(Integer page){
        Page<Order> page1 = new Page<>(page, 2);
        Page<Order> orderList = orderService.findAll(page1);

        for (Order order : orderList.getRecords()) {
            User all = client.findByid(order.getUserId());
            order.setUser(all);
        }
        return orderList;
    }
    @PostMapping("/del")
    public void del(Long userId){
        orderService.del(userId);
    }
    @PostMapping("/add")
    public void add(@RequestBody Order order){
        orderService.add(order);
    }
    @GetMapping("/alll")
    public List<User> alll(){
        return client.findAll();
    }

}
